layui.define(['table', 'form'], function (exports) {
    var $ = layui.$
        , admin = layui.admin
        , view = layui.view
        , table = layui.table
        , form = layui.form;

    //导航管理
    table.render({
        elem: '#LAY-set-nav-list'
        , url: layui.setter.host + '/nav'
        , cols: [[
            {type: 'checkbox', fixed: 'left'}
            , {field: 'id', width: 100, title: 'ID', sort: true}
            , {field: 'name', title: '名称', minWidth: 100}
            , {field: 'sort', width: 80, title: '排序'}
            , {field: 'status', title: '状态', templet: '#statusTpl', width: 80, align: 'center'}
            , {field: 'create_time', title: '创建时间', sort: true}
            , {title: '操作', width: 250, align: 'center', fixed: 'right', toolbar: '#LAY-set-nav-bar'}
        ]]
        , page: true
        , limit: 30
        , height: 'full-320'
        , text: '对不起，加载出现异常！'
    });

    //监听工具条
    table.on('tool(LAY-set-nav-list)', function (obj) {
        var data = obj.data;
        if (obj.event === 'del') {
            layer.prompt({
                formType: 1
                , title: '敏感操作，请验证口令'
            }, function (value, index) {
                layer.close(index);

                layer.confirm('真的删除行么', function (index) {
                    admin.req({
                        url: layui.setter.host + '/nav/' + data.id
                        , type: "delete"
                        , done: function (res) {
                            obj.del();
                            layer.close(index);
                        }
                    });
                });
            });
        } else if (obj.event === 'edit') {
            admin.popup({
                title: '编辑幻灯片'
                , area: ['400px', '300px']
                , id: 'LAY-set-slide-edit'
                , success: function (layero, index) {
                    view(this.id).render('set/nav/listform', data).done(function () {
                        form.render(null, 'LAY-set-nav-form');

                        //监听提交
                        form.on('submit(LAY-set-nav-submit)', function (form) {
                            admin.req({
                                url: layui.setter.host + '/nav/' + data.id
                                , data: form.field
                                , type: "put"
                                , done: function (res) {
                                    obj.update(res.data); //数据更新
                                    layer.close(index); //执行关闭
                                }
                            });
                        });
                    });
                }
            });
        }
    });

    //导航菜单管理
    table.render({
        elem: '#LAY-set-menu-list'
        , url: layui.setter.host + '/nav/' + layui.router().search.nav_id + '/menu'
        , cols: [[
            {type: 'checkbox', fixed: 'left'}
            , {field: 'id', width: 100, title: 'ID', sort: true}
            , {field: 'name', title: '菜单名', minWidth: 100}
            , {field: 'icon', title: '图标', width: 100, templet: '#imgTpl'}
            , {field: 'url', title: '地址'}
            , {field: 'sort', width: 80, title: '排序'}
            , {field: 'status', title: '状态', templet: '#statusTpl', width: 80, align: 'center'}
            , {field: 'create_time', title: '创建时间', sort: true}
            , {title: '操作', width: 150, align: 'center', fixed: 'right', toolbar: '#LAY-set-menusbar'}
        ]]
        , page: true
        , limit: 30
        , height: 'full-320'
        , text: '对不起，加载出现异常！'
    });

    //监听工具条
    table.on('tool(LAY-set-menu-list)', function (obj) {
        var data = obj.data;
        if (obj.event === 'del') {
            layer.prompt({
                formType: 1
                , title: '敏感操作，请验证口令'
            }, function (value, index) {
                layer.close(index);

                layer.confirm('真的删除行么', function (index) {
                    admin.req({
                        url: layui.setter.host + '/nav/' + layui.router().search.nav_id + '/menu/' + data.id
                        , type: "delete"
                        , done: function (res) {
                            obj.del();
                            layer.close(index);
                        }
                    });
                });
            });
        } else if (obj.event === 'edit') {
            admin.popup({
                title: '编辑菜单'
                , area: ['500px', '450px']
                , id: 'LAY-set-menu-edit'
                , success: function (layero, index) {
                    view(this.id).render('set/menu/menuform', data).done(function () {
                        form.render(null, 'LAY-set-menu-form');

                        //监听提交
                        form.on('submit(LAY-set-menu-submit)', function (form) {
                            admin.req({
                                url: layui.setter.host + '/nav/' + layui.router().search.nav_id + '/menu/' + data.id
                                , data: form.field
                                , type: "put"
                                , done: function (res) {
                                    obj.update(res.data); //数据更新
                                    layer.close(index); //执行关闭
                                }
                            });
                        });
                    });
                }
            });
        }
    });

    exports('nav', {})
});